package it.afs.worklog.application.form.admin;

import java.util.Arrays;
import java.util.List;

import it.afs.utils.PropertyManager;
import it.afs.worklog.User;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;

/**
 * @author Fanta
 */
public class UtenteFormPage extends AdminPage
{

	public UtenteFormPage()
	{
		super();
		addComponents(new User());
	}

	public UtenteFormPage(PageParameters parameters)
	{
		super(parameters);
		addComponents(getFactory().getDao().getUser(parameters.getString("id")));
	}
	
	private void addComponents(User object)
	{
		addComponents();
		
		add(new Link<Void>("back")
		{

			/**
			 * 
			 */
			private static final long serialVersionUID = 7255350471067915303L;

			@Override
			public void onClick()
			{
				setResponsePage(UtentiPage.class);
			}
		});
		
		if(object.isNew())
		{
			object.setPassword(PropertyManager.getValue("user.password.default"));
		}
		
		Form<User> form = new Form<User>("form", new CompoundPropertyModel<User>(object))
		{
			private static final long serialVersionUID = 3590784864715504120L;

			@Override
			protected void onSubmit()
			{
				super.onSubmit();
				
				if(getModelObject().isNew() && getFactory().getDao().existUser(getModelObject().getUsername()))
				{
					error(getString("user.exist"));
				}
				else if(getFactory().getDao().set(getModelObject()))
				{
					setResponsePage(UtentiPage.class);
				}
				else
				{
					error(getString("submit.error"));
				}
			}
		};
		add(form);
		
		form.add(new RequiredTextField<String>("username").setEnabled(object.isNew()));
		form.add(new RequiredTextField<String>("nome"));
		
		List<Integer> livelli = Arrays.asList(new Integer[]{0, 1, 2, -1});
		DropDownChoice<Integer> livello = new DropDownChoice<Integer>("livello", livelli,
			new IChoiceRenderer<Integer>()
			{
				private static final long serialVersionUID = 7008875083171006124L;
	
				@Override
				public Object getDisplayValue(Integer object) {
					return new StringResourceModel("role.${intValue}", new Model<Integer>(object)).getString();
				}
	
				@Override
				public String getIdValue(Integer object, int index) {
					return String.valueOf(index);
				}
			
			});
		livello.setRequired(true);
		form.add(livello);
		
		form.add(new TextField<String>("cf"));
		form.add(new TextField<String>("qualifica"));
		form.add(new TextField<String>("contratto"));
		form.add(new TextField<String>("contatto"));
		form.add(new TextField<String>("costo"));
		
		form.add(new FeedbackPanel("feedback"));
	}
}